﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PicTool
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static void ModifyDpi(string sourcePath, float newDpi)
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(sourcePath);
                FileSystemInfo[] files = dir.GetFileSystemInfos();//是目录
                if (files.Length > 0)
                {
                    for (int i = 0; i < files.Length; i++)
                    {
                        FileInfo file = files[i] as FileInfo;
                        if (file != null)//是文件  
                        {
                            using (Image image = Image.FromFile(file.FullName))
                            {
                                string extension = Path.GetExtension(file.FullName);
                                using (Bitmap bmp = new Bitmap(image.Width, image.Height))
                                {
                                    using (Graphics g = Graphics.FromImage(bmp))
                                    {
                                        // 设置绘图质量
                                        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

                                        // 将原始图片绘制到新的Bitmap对象上
                                        g.DrawImage(image, 0, 0);

                                        // 设置新的DPI
                                        bmp.SetResolution(newDpi, newDpi);

                                        // 保存修改后的图片
                                        string newPath = file.FullName.Replace(extension, "_modified" + extension); // 假设修改后的图片是JPEG格式
                                        bmp.Save(newPath, ImageFormat.Jpeg);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnFile_Click(object sender, EventArgs e)
        {
            using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
            {
                folderBrowserDialog.Description = "选择文件夹";
                DialogResult dialogResult = folderBrowserDialog.ShowDialog();

                if (dialogResult == DialogResult.OK)
                {
                    string folderPath = folderBrowserDialog.SelectedPath;
                    textBox1.Text = folderPath;
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(txtDpi.Text))
            {
                string sourcePath = textBox1.Text;
                if(!float.TryParse(txtDpi.Text,out float newDpi))
                {
                    MessageBox.Show("分辨率格式不正确！");
                    return;
                }
                ModifyDpi(sourcePath, newDpi);
                MessageBox.Show("操作完成！");
            }
            else
            {
                MessageBox.Show("新分辨率与源图片目录不能为空！");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}